package com.hh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.entity.Login;
import com.hh.entity.Student;
import com.hh.entity.Teacher;
import com.hh.entity.vo.LoginModifyPasswordVO;
import com.hh.entity.vo.LogininfoVO;
import com.hh.entity.vo.StudentLoginVO;
import com.hh.entity.vo.TeacherLoginVO;
import com.hh.mapper.LoginMapper;
import com.hh.service.LoginService;
import com.hh.service.StudentService;
import com.hh.service.TeacherService;
import com.hh.utils.ObjectUtils;
import com.hh.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Login)表服务实现类
 *
 * @author makejava
 * @since 2022-10-15 13:08:46
 */
@Service("loginService")
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements LoginService {
    @Autowired
    LoginService loginService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    LoginMapper loginMapper;
    @Override
    public StudentLoginVO studentLogin(LogininfoVO login) {
        StudentLoginVO studentdto = new StudentLoginVO();
        String userid = login.getLoginId();
        Integer roleId = login.getRoleId();
        String pwd = login.getLoginPwd();
        String code = login.getFcode();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Login_id",userid);
        queryWrapper.eq("Login_pwd",pwd);
        queryWrapper.eq("Role_id",roleId);
        //登录信息查询
        Login logininfo = loginService.getOne(queryWrapper);

        if (!ObjectUtils.isEmpty(logininfo)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("Login_id",logininfo.getLoginId());
            Student student = studentService.getOne(wrapper);

            studentdto = studentdto.copystudent(student);
            studentdto.setFcode(code);
            return studentdto;
        }

        return studentdto;
    }

    @Override
    public TeacherLoginVO teacherLogin(LogininfoVO login) {
        TeacherLoginVO teacherdto = new TeacherLoginVO();
        String userid = login.getLoginId();
        Integer roleId = login.getRoleId();
        String pwd = login.getLoginPwd();
        String code = login.getFcode();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Login_id",userid);
        queryWrapper.eq("Login_pwd",pwd);
        queryWrapper.eq("Role_id",roleId);
        //登录信息查询
        Login logininfo = loginService.getOne(queryWrapper);

        if (!ObjectUtils.isEmpty(logininfo)){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("Login_id",logininfo.getLoginId());
            Teacher teacher = teacherService.getOne(wrapper);

            teacherdto = teacherdto.copyteacher(teacher);
            teacherdto.setFcode(code);
            return teacherdto;
        }

        return teacherdto;
    }

    @Override
    public Integer modifyPassword(LoginModifyPasswordVO loginModifyPassworddto) {
        int i=0;
        String loginId = loginModifyPassworddto.getLoginId();
        String loginPwd = loginModifyPassworddto.getLoginPwd();
        Integer roleId = loginModifyPassworddto.getRoleId();
        String newpwd = loginModifyPassworddto.getNewpwd();
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("Login_id",loginId);
        wrapper.eq("Login_pwd",loginPwd);
        wrapper.eq("Role_id",roleId);
        Login loginbyId = loginService.getOne(wrapper);
        //如果查到该用户，更新密码
        if (!ObjectUtils.isEmpty(loginbyId)){
            UpdateWrapper wrapper1 = new UpdateWrapper();
            wrapper1.eq("Login_id",loginId);
            wrapper1.eq("Login_pwd",loginPwd);
            wrapper1.eq("Role_id",roleId);
            wrapper1.set("Login_pwd",newpwd);
            i = loginMapper.update(loginbyId,wrapper1);
        }
        return i;
    }
}
